package org.krayne.swivel.filter;



/**
 * A simple {@code StringFilter} that filters based on a specified string
 * and case sensitivity.
 * 
 * @author dhsu
 */
public class SimpleStringFilter implements StringFilter {
    /**
     * A default filter that passes every string.
     */
    public static final SimpleStringFilter ALL_PASS = new SimpleStringFilter("", false);
    
    // filter criteria
    private final String filterString;
    private boolean isCaseSensitive;
    
    /**
     * Constructs a simple string filter that filters based on a string and
     * case sensitivity.
     * 
     * @param filterString the filter string
     * @param isCaseSensitive whether the filter is case sensitive or not
     */
    public SimpleStringFilter(String filterString, boolean isCaseSensitive) {
        this.filterString = filterString;
        this.isCaseSensitive = isCaseSensitive;
    }
    
    /**
     * {@inheritDoc}
     */
    public boolean pass(String s) {
        if(isCaseSensitive) {
            if(s.indexOf(filterString) != -1) {
                return true;
            }
        } else {
            if(s.toLowerCase().indexOf(filterString.toLowerCase()) != -1) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * Gets the filter string.
     * 
     * @return the filter string
     */
    public String getFilterString() {
        return this.filterString;
    }
    
    /**
     * Gets whether or not this filter is case sensitive.
     * 
     * @return case sensitivity of this filter
     */
    public boolean isCaseSensitive() {
        return this.isCaseSensitive;
    }

    /**
     * Generated by Eclipse.
     * 
     * {@inheritDoc}
     */
    public int hashCode() {
        final int PRIME = 31;
        int result = 1;
        result = PRIME * result + ((filterString == null) ? 0 : filterString.hashCode());
        result = PRIME * result + (isCaseSensitive ? 1231 : 1237);
        return result;
    }

    /**
     * Generated by Eclipse.
     * 
     * {@inheritDoc}
     */
    public boolean equals(Object obj) {
        if(this == obj) {
            return true;
        } else if(obj == null) {
            return false;
        }
        
        if(getClass() != obj.getClass()) {
            return false;
        }
        
        final SimpleStringFilter other = (SimpleStringFilter) obj;
        if(filterString == null) {
            if(other.filterString != null) {
                return false;
            }
        } else if(!filterString.equals(other.filterString)) {
            return false;
        }
        
        if(isCaseSensitive != other.isCaseSensitive) {
            return false;
        }
        
        return true;
    }    
}
